package com.beerandjesus.sportofkings.handicapper;

import com.beerandjesus.sportofkings.handicapper.app.CardAnalyzer;
import com.beerandjesus.sportofkings.handicapper.app.CardAnalyzerException;
import com.beerandjesus.sportofkings.handicapper.factory.ResultsTableFactory;
import com.beerandjesus.sportofkings.handicapper.model.RaceAnalysisResultVO;
import com.beerandjesus.sportofkings.handicapper.model.ResultsTable;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.CardVOImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Main class for -jar execution.
 *
 */
public class Main 
{
	private ApplicationContext context;
    private String filePath;

    private static final String[] CONTEXTS = new String[] {"spring-config.xml",
            "applicationContext-handicapper-jar.xml"};
    private static final String BUILD_JOCKEY_DATABASE = "-buildJockeyDatabase";
    private static final String ADD_JOCKEYS = "-addJockeys";

    public static void main( String[] args ) {
        if (args == null || args.length < 1 || args.length > 2) {
            printUsage();
            return;
        }
        String ppFilePath = args[0];
        if (args.length == 2) {
            if (args[1].equals(BUILD_JOCKEY_DATABASE) ||
                    args[1].equals(ADD_JOCKEYS)) {
                ApplicationContext context = new ClassPathXmlApplicationContext(CONTEXTS);
                JockeyDatabaseUtil main = new JockeyDatabaseUtil();
                main.setApplicationContext(context);
                main.setFilePath(ppFilePath);
                if (args[1].equals(BUILD_JOCKEY_DATABASE)) {
                    main.rebuild();
                } else if (args[1].equals(ADD_JOCKEYS)) {
                    main.execute();
                } else {
                    printUsage();
                }
            } else {
                printUsage();
            }
        } else {
            ApplicationContext context = new ClassPathXmlApplicationContext(CONTEXTS);
            Main main = new Main();
            main.setApplicationContext(context);
            main.setFilePath(ppFilePath);
            main.execute();
        }
    }

    private static void printUsage() {
        System.out.println("Usage: java -jar <jarfile> <path to past performances file> [option]\n" +
                "Option:\n" +
                "-buildJockeyDatabase   (Tears down and rebuilds the jockey rating databse)\n" +
                "-addJockeys            (Adds jockey ratings to an existing jockey rating database)");
    }

    public void execute() {
        CardAnalyzer cardAnalyzer = (CardAnalyzer) context.getBean("cardAnalyzer");
        ResultsTableFactory resultsTableFactory = (ResultsTableFactory) context.getBean("resultsTableFactory");
        CardVOImpl card = new CardVOImpl();
        card.setFilePath(getFilePath());
        List<RaceAnalysisResultVO> races;
        try {
            races = cardAnalyzer.analyzeCard(card);
        } catch (CardAnalyzerException e) {
            e.printStackTrace();
            return;
        }
        System.out.print(resultsTableFactory.getOutputSimpleFormat(races));

    }

    public void setApplicationContext(ApplicationContext context) {
    	this.context = context;
    }
    
    public ApplicationContext getApplicationContext() {
    	return context;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
}
